package com.xutong.myfunction.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.xutong.myfunction.R;
import com.xutong.myfunction.adapter.RVAdapter;
import com.xutong.myfunction.custom.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<String> mDatas;
    private RVAdapter rvAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        initData();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()){
                    case R.id.menuAdd:
                        rvAdapter.addData(5);
                        break;
                    case R.id.menuDelete:
                        rvAdapter.removeData(5);
                        break;
                }

                return true;
            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.rvRecyclerview);
//        recyclerView.setLayoutManager(new LinearLayoutManager(this));//线性布局
        recyclerView.setLayoutManager(new GridLayoutManager(this,4));//网格布局
        //也是网格布局，可以随意变化，在onBindViewHolder方法中为我们的item设置个随机的高度，效果就是瀑布布局了
//        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));
        rvAdapter = new RVAdapter(this,mDatas);
        recyclerView.setAdapter(rvAdapter);
//        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));//线性布局的分割线类
        recyclerView.addItemDecoration(new DividerGridItemDecoration(this));//网格布局的分割线类

        rvAdapter.setOnItemClickLitener(new RVAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {

                Toast.makeText(RecyclerViewActivity.this,"点击了第"+(position+1)+"个",Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(RecyclerViewActivity.this,"长按了第"+(position+1)+"个",Toast.LENGTH_SHORT).show();

            }
        });

    }

    protected void initData()
    {
        mDatas = new ArrayList<String>();
        for (int i = 'A'; i < 'z'; i++)
        {
            mDatas.add("" + (char) i);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_recyclerview,menu);

        return super.onCreateOptionsMenu(menu);
    }
}
